/**
 * Description:
 *
 * @author      Eden
 * @datetime    2016/11/25 19:58
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */


(function () {

    "use strict";

    var api = {
        getUserDetailUrl: '/user/detail/getUserInfo',
        getOrdersUrl: '/user/detail/getOrderLists',
        getBbsUrl: '/user/detail/getBbsLists',
        getWxLevelUrl: '/user/detail/getWxLists',
        getCollectUrl: '/user/detail/getCollect',
        getCashInUrl: '/user/detail/cashin',
        getCashOutUrl: '/user/detail/cashout'
    };

    sm.controller("admin-user-detail-controller", ['$scope', '$http', 'Modal', function ($scope, $http, Modal) {


        $scope.detail = {};
        $scope.order = [];
        $scope.selectType = ['order', 'bbs', 'wx'];
        $scope.nowType = 'order';
        $scope.bbsLists = [];
        $scope.bbsLoadMore = false;
        $scope.bbsPage = 1;

        $scope.wxLevel1 = [];
        $scope.wxloadMoreL1 = false;
        $scope.wxPage1 = 1;
        $scope.wxLevel2 = [];
        $scope.wxloadMoreL2 = false;
        $scope.wxPage2 = 1;

        $scope.address = [];
        $scope.cashin = [];
        $scope.cashout = [];

        $scope.tabType = 'profile';
        $scope.showEmpty = false;

        /**
         * 获取用户信息
         */
        $http.get(api.getUserDetailUrl, {
            params: {
                userId: GetQueryString('id')
            }
        }).success(function (res) {
            if (res.state) {
                $scope.detail = res.data;
            }
        });

        // 地址
        $scope.getAddress = function () {
            $scope.showEmpty = false;
            $http.get('/user/detail/getAddress', {
                params: {
                    userId: GetQueryString('id')
                }
            }).success(function (res) {
                if (res.state) {
                    if (!res.data.length || res.data.length == 0) {
                        $scope.showEmpty = true;
                    }
                    $scope.address = res.data;
                }
            });
        };

        $scope.select = function (type) {
            if ($scope.nowType == type) {
                return false;
            }

            $scope.nowType = type;
            if (type == 'bbs') {
                if ($scope.bbsLists.length > 0) {
                    return false;
                }
                doBbsSelect();
            }

            if (type == 'wx_1') {
                if ($scope.wxLevel1.length > 0) {
                    return false;
                }
                doWxSelect(1);
            }

            if (type == 'wx_2') {
                if ($scope.wxLevel2.length > 0) {
                    return false;
                }
                doWxSelect(2);
            }
        };

        /**
         * 微信搜索
         * @param level
         */
        function doWxSelect(level)
        {
            $scope.tabType = 'level-' + level;
            $scope.showEmpty = false;

            $http.get(api.getWxLevelUrl, {
                params: {
                    userId: GetQueryString('id'),
                    page: level == 1 ? $scope.wxPage1 : $scope.wxPage2,
                    level: level
                }
            }).success(function (res) {
                if (res.state) {
                    if (!res.data.total || res.data.total == 0) {
                        $scope.showEmpty = true;
                    }

                    if (level == 1) {
                        appendLists($scope.wxLevel1, res.data.lists);
                        $scope.wxloadMoreL1 = $scope.wxLevel1.length < res.data.total;
                    }

                    if (level == 2) {
                        appendLists($scope.wxLevel2, res.data.lists);
                        $scope.wxloadMoreL2 = $scope.wxLevel2.length < res.data.total;
                    }
                }
            });
        }

        /**
         * 获取订单
         */
        $scope.getOrder = function () {
            $scope.tabType = 'order';
            $http.get(api.getOrdersUrl, {
                params: {
                    page: 1,
                    userId: GetQueryString('id')
                },
                cache: true
            }).success(function (res) {
                if (res.state) {
                    if (!res.data) {
                        $scope.showEmpty = true;
                    }
                    $scope.order = res.data;
                }
            });
        };

        $scope.switchTab = function (tabName) {
            $scope.tabType = tabName;
            $scope.showEmpty = false;
            switch (tabName) {
                case "profile":
                    break;
                case "order":
                    $scope.getOrder();
                    break;
                case "bbs":
                    doBbsSelect();
                    break;
                case "collect":
                    $scope.getCollect();
                    break;
                case "level-1":
                    doWxSelect(1);
                    break;
                case "level-2":
                    doWxSelect(2);
                    break;
                case "cashin":
                    $scope.getCashIn();
                    break;
                case "cashout":
                    $scope.getCashout();
                    break;
                case "wallet":
                    $scope.getWallet();
                    break;
                case "address":
                    $scope.getAddress();
                    break;
                case "money":
                    break;
            }
        };

        $scope.getCashout = function () {
            $scope.showEmpty = false;
            $http.get(api.getCashOutUrl, {
                params: {
                    page: 1,
                    userid: GetQueryString('id')
                }
            }).success(function (res) {
                if (res.state) {
                    if (res.data.length == 0) {
                        $scope.showEmpty = true;
                        return false;
                    }

                    $scope.cashout = res.data;
                }
            });
        };

        $scope.getCashIn = function () {
            $scope.showEmpty = false;

            $http.get(api.getCashInUrl, {
                params: {
                    page: 1,
                    userid: GetQueryString('id')
                }
            }).success(function (res) {
                if (res.state) {
                    if (res.data.length == 0) {
                        $scope.showEmpty = true;
                        return false;
                    }

                    $scope.cashin = res.data;
                }
            });
        };

        /**
         * 搜索用户发帖
         * @returns {boolean}
         */
        function doBbsSelect()
        {
            $scope.tabType = 'bbs';
            $scope.showEmpty = false;
            $http.get(api.getBbsUrl, {
                params: {
                    userId: GetQueryString('id'),
                    page: $scope.bbsPage
                },
                cache: true
            }).success(function (res) {
                if(res.state) {
                    if (res.data.total == 0) {
                        $scope.showEmpty = true;
                        return ;
                    }
                    appendLists($scope.bbsLists, res.data.data);
                    $scope.bbsLoadMore = res.data.nextPage;
                }
            });
        }


        // 收藏
        $scope.getCollect = function () {
            $scope.tabType = 'collect';
            $scope.showEmpty = false;
            $http.get(api.getCollectUrl, {
                params: {
                    page: 1,
                    userId: $scope.detail.userId
                }
            }).success(function (res) {
                if (res.state) {
                    if (!res.data) {
                        $scope.showEmpty = true;
                        return ;
                    }
                    $scope.collect = res.data.data;
                }
            });
        };




        /**
         * 加载更多
         * @returns {boolean}
         */
        $scope.loadMoreBbs = function () {
            if (!$scope.bbsLoadMore) {
                return false;
            }

            $scope.bbsPage += 1;
            doBbsSelect();
        };

        function appendLists(toLists, lists) {
            if (lists.length == 0) {
                return false;
            }

            angular.forEach(lists, function (v) {
                toLists.push(v);
            });
        }

        /**
         * 微信号加载
         * @param level
         * @returns {boolean}
         */
        $scope.loadMoreWx = function (level) {
            if ((level == 1 && !$scope.wxPage1) || (level == 2 && !$scope.wxPage2)) {
                return false;
            }

            doWxSelect(level);
        }

    }]);



})();
